from django.db import models
import timeago, datetime

from neuproject import settings


class Comment(models.Model):
    entry = models.ForeignKey(settings.COMMENT_ENTRY_MODEL, related_name='entry_comments')
    content = models.TextField()
    author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='user_comments', blank=True, null=True)
    submit_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['submit_date']

    def __str__(self):
        return '%s 评论了 %s' % (self.author.username, self.entry)

    def get_timeago(self):
        now = datetime.datetime.now()
        date = self.submit_date.strftime('%Y-%m-%d %H:%M:%S')
        return timeago.format(date, now, 'zh_CN')


class CommentReply(models.Model):
    comment = models.ForeignKey(Comment, related_name='comment_reply', blank=True, null=True)
    content = models.TextField()
    reply_from = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='user_reply', blank=True, null=True)
    reply_to = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
    submit_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['submit_date']

    def __str__(self):
        return '%s @ %s ' % (self.reply_from.username, self.reply_to.username)

    def get_timeago(self):
        now = datetime.datetime.now()
        date = self.submit_date.strftime('%Y-%m-%d %H:%M:%S')
        return timeago.format(date, now, 'zh_CN')
